class AudioAssetsController < ApplicationController
  include ApplicationHelper
  
  before_filter :login_required, :except => [:index, :show, :search_tagged]
  before_filter :body_id
  
  def body_id
    @body_id = :audio_assets
  end
  
  # GET /audio_assets
  # GET /audio_assets.:format
  def index
    @audio_assets = AudioAsset.find :all, :order => :name
	
    respond_to do |format|
      format.html
      format.xml { render :xml => @audio_assets.to_xml( :except => [:updated_at, :expiration_date] ) }
    end
  end
  
  # GET /audio_assets/new
  def new
    #@audio_asset = AudioAsset.new
  
    @formats = AudioAsset::FORMATS
  	@genres = AudioAsset::GENRES
  	@all_authors = User.find_all_authors
  	@all_authors.delete current_user
  end
  
  # POST /audio_assets
  # POST /audio_assets.:format
  def create
    @audio_asset = AudioAsset.new(params[:audio_asset])
	
  	respond_to do |format|
  	  if @audio_asset.save
  	    flash[:notice] = "Success on creating the new audio asset"
  	    associate_authors
    		format.html { redirect_to audio_asset_path(@audio_asset) }
    		format.xml { head :ok }
  	  else
  	    flash[:error] = "Sorry, this audio asset can't be created. Try again"
    		format.html { render :action => 'new' }
    		format.xml { head :xml => @audio_asset.errors.to_xml }
  	  end
  	end
  end
  
   
  # GET /audio_assets/:id
  # GET /audio_assets/:id.:format
  def show
    respond_to do |format|
      begin
        @audio_asset = AudioAsset.find params[:id]
        @active_replication_systems = @audio_asset.find_where_asset_is_replicated
        @inactive_replication_systems = @audio_asset.find_where_asset_is_not_replicated
        @programs = @audio_asset.programs
        @authors = @audio_asset.authors
        
        format.html { @tags = @audio_asset.tag_cloud }
        format.xml { render :xml => @audio_asset.to_xml( :except => [:updated_at, :expiration_date] ) }
      rescue
        flash[:error] = "Sorry, the Audio Asset doesn't exist"
        format.html { redirect_to :action => 'index' }
        format.xml { head :ok }
      end
    end
  end
  
  def download
    @audio_asset = AudioAsset.find params[:id]
  	begin
  	  # audio_asset.download
  	  send_file @audio_asset.download, :filename => (@audio_asset.filename + '.' + @audio_asset.format)
  	  # x_send_file audio_asset.download, :filename => 'test.mp3'
  	  @audio_asset.download_counter += 1
  	  @audio_asset.save
  	rescue
  	  flash[:error] = "Sorry, it's impossible to download this audio asset"
      redirect_to :action => 'index'
  	end
  end
  
  def download_metadata
    @audio_asset = AudioAsset.find params[:id]
  	begin
  	  send_file @audio_asset.download_metadata, :filename => (@audio_asset.filename + '.xml')
  	rescue
  	  flash[:error] = "Sorry, it's impossible to download the metadata"
      redirect_to :action => 'index'
  	end
  end
  
  def tagging
    @audio_asset = AudioAsset.find params[:tagging][:resource_id]
    if @audio_asset
      @audio_asset.add_tags(params[:tagging][:tags])
      @audio_asset.save
      
      @tags = @audio_asset.tag_cloud
    else
      @tags = []
    end
    
    render :partial => 'shared/tag_cloud', :locals => { :tags => @tags, :controller => 'audio_assets' }
  end
  
  def search_tagged
    @audio_assets = AudioAsset.find_by_tags params[:tag]
    @all_tags = AudioAsset.tag_cloud
    @tag = params[:tag]
  end
  
  def begin_replication
    replication_system = ReplicaSystem.find params[:replica_system_id]
    audio_asset = AudioAsset.find params[:audio_asset_id]
    
    audio_asset.replicate(replication_system)
    
    render :partial => 'replication', :locals => { :audio_asset => audio_asset, 
      :active_replication_systems => audio_asset.find_where_asset_is_replicated,
      :inactive_replication_systems => audio_asset.find_where_asset_is_not_replicated }
  end
  
  def stop_replication
    replication_system = ReplicaSystem.find params[:replica_system_id]
    audio_asset = AudioAsset.find params[:audio_asset_id]
    
    audio_asset.remove_replication(replication_system)
    
    render :partial => 'replication', :locals => { :audio_asset => audio_asset, 
      :active_replication_systems => audio_asset.find_where_asset_is_replicated,
      :inactive_replication_systems => audio_asset.find_where_asset_is_not_replicated }
  end
  
  private
  def associate_authors
    User.enable_asset_authorship(current_user.id, @audio_asset) if current_user
	
    params[:authors].each do |asset, value|
      User.enable_asset_authorship(value, @audio_asset) if value != ''
    end
  end
end
